package com.zhongshengnetwork.rightbe.dbservice;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.LogindbModel;
import com.zhongshengnetwork.rightbe.dbmodel.PicdbModel;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by zhongsheng-luojianlan on 17/1/2.
 */

public class PicdbService {
    //插入或者更新数据
    public static boolean saveOrUpdate(PicdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //查找数据
    public static List<PicdbModel> getPicList(){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(PicdbModel.class) != null){
                List<PicdbModel> list = db.selector(PicdbModel.class).where("userid","==",CustomApplication.loginModel.getUid()).findAll();
                return list;
            }
            return null;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //查找数据是否存在
    public static boolean isHasById(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.findById(PicdbModel.class,id) == null){
                return false;
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除数据
    public static boolean deleteById(String id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(PicdbModel.class,id);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }
}
